
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>天空盒和水面渲染 - 浪子之心</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Blog,"> 
    <meta name="description" content="MrChen Bolg,天空球防止被相机裁剪
12345#if UNITY_REVERSED_Z    o.vertex.z = o.vertex.w * 0.000001f;#else    o.vertex.z = o,"> 
    <meta name="author" content="Jack Chen"> 
    <link rel="alternative" href="atom.xml" title="浪子之心" type="application/atom+xml"> 
    <link rel="icon" href="/blog/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/blog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.1"><link rel="stylesheet" href="/blog/css/prism.css" type="text/css"></head>

<body class="loading">
    <span id="config-title" style="display:none">浪子之心</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://mrchenlearnspace.github.io/blog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">天空盒和水面渲染</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">天空盒和水面渲染</h1>
        <div class="stuff">
            <span>七月 04, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/Shader/" rel="tag">Shader</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="天空球"><a href="#天空球" class="headerlink" title="天空球"></a>天空球</h1><p>防止被相机裁剪</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">if</span> UNITY_REVERSED_Z</span></span><br><span class="line">    o.vertex.z = o.vertex.w * <span class="number">0.000001f</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">    o.vertex.z = o.vertex.w * <span class="number">0.999999f</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<p>渲染队列改成1000左右。</p>
<h1 id="水面"><a href="#水面" class="headerlink" title="水面"></a>水面</h1><h2 id="水面反射"><a href="#水面反射" class="headerlink" title="水面反射"></a>水面反射</h2><p>在平面的反射面建立一个摄像头，将渲染的Texture直接采样到平面上达到映射的效果，通过clipPlaneOffset 修正物体在通过改变反射矩阵修改具体位置。<br><img src="https://img-blog.csdnimg.cn/b9139b712bcd454488d98f3239a2bf12.png" alt="在这里插入图片描述"></p>
<p>开抄<br>PlanarReflection.cs</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> UnityEngine.Rendering;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[<span class="meta">ExecuteInEditMode</span>]</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">PlanarReflection</span> : <span class="title">MonoBehaviour</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> LayerMask _reflectionMask = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">bool</span> _reflectSkybox = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _clipPlaneOffset = <span class="number">0.07F</span>;</span><br><span class="line">    <span class="comment">//反射图属性名</span></span><br><span class="line">    <span class="keyword">const</span> <span class="built_in">string</span> _reflectionTex = <span class="string">&quot;_ReflectionTex&quot;</span>;</span><br><span class="line">    Camera _reflectionCamera;</span><br><span class="line">    Vector3 _oldpos;</span><br><span class="line">    RenderTexture _bluredReflectionTexture;</span><br><span class="line">    Material _sharedMaterial;</span><br><span class="line">    <span class="comment">//模糊效果相关参数</span></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">bool</span> _blurOn = <span class="literal">true</span>;</span><br><span class="line">    [<span class="meta">Range(0.0f, 5.0f)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _blurSize = <span class="number">1</span>;</span><br><span class="line">    [<span class="meta">Range(0, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> _blurIterations = <span class="number">2</span>;</span><br><span class="line">    [<span class="meta">Range(1.0f, 4.0f)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _downsample = <span class="number">1</span>;</span><br><span class="line">    <span class="comment">//记录上述模糊参数，用于判断参数是否发生变化   </span></span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">bool</span> _oldBlurOn;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">float</span> _oldBlurSize;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">int</span> _oldBlurIterations;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">float</span> _oldDownsample;</span><br><span class="line">    <span class="comment">//模糊shader</span></span><br><span class="line">    <span class="keyword">private</span> Shader _blurShader;</span><br><span class="line">    <span class="keyword">private</span> Material _blurMaterial;</span><br><span class="line">    <span class="comment">//用来判断当前是否正在渲染反射图</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="built_in">bool</span> _insideRendering;</span><br><span class="line"></span><br><span class="line">    Material BlurMaterial &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (_blurMaterial == <span class="literal">null</span>) &#123;</span><br><span class="line">                _blurMaterial = <span class="keyword">new</span> Material(_blurShader);</span><br><span class="line">                <span class="keyword">return</span> _blurMaterial;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> _blurMaterial;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Awake</span>()</span> &#123;</span><br><span class="line">        _oldBlurOn = _blurOn;</span><br><span class="line">        _oldBlurSize = _blurSize;</span><br><span class="line">        _oldBlurIterations = _blurIterations;</span><br><span class="line">        _oldDownsample = _downsample;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Start</span>()</span> &#123;</span><br><span class="line">        _sharedMaterial = GetComponent&lt;MeshRenderer&gt;().sharedMaterial;</span><br><span class="line">        _blurShader = Shader.Find(<span class="string">&quot;Hidden/KawaseBlur&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> (_blurShader == <span class="literal">null</span>)</span><br><span class="line">            Debug.LogError(<span class="string">&quot;缺少Hidden/KawaseBlur Shader&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">bool</span> _blurParamChanged;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Update</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (_blurParamChanged) &#123;</span><br><span class="line">            _oldBlurOn = _blurOn;</span><br><span class="line">            _oldBlurSize = _blurSize;</span><br><span class="line">            _oldBlurIterations = _blurIterations;</span><br><span class="line">            _oldDownsample = _downsample;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (_blurOn != _oldBlurOn || _blurSize != _oldBlurSize || _blurIterations != _oldBlurIterations || _downsample!= _oldDownsample)</span><br><span class="line">        &#123;</span><br><span class="line">            _blurParamChanged = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建反射用的摄像机</span></span><br><span class="line">    <span class="function">Camera <span class="title">CreateReflectionCamera</span>(<span class="params">Camera cam</span>)</span> &#123;</span><br><span class="line">        <span class="comment">//生成Camera</span></span><br><span class="line">        String reflName = gameObject.name + <span class="string">&quot;Reflection&quot;</span> + cam.name; </span><br><span class="line">        GameObject go = <span class="keyword">new</span> GameObject(reflName);</span><br><span class="line">        <span class="comment">//go.hideFlags = HideFlags.HideAndDontSave;</span></span><br><span class="line">        go.hideFlags = HideFlags.HideAndDontSave;</span><br><span class="line">       </span><br><span class="line">        Camera reflectCamera = go.AddComponent&lt;Camera&gt;();</span><br><span class="line">        <span class="comment">//设置反射相机的参数</span></span><br><span class="line">        HoldCameraSettings(reflectCamera);</span><br><span class="line">        <span class="comment">//创建RT并绑定Camera</span></span><br><span class="line">        <span class="keyword">if</span> (!reflectCamera.targetTexture) &#123;</span><br><span class="line">            reflectCamera.targetTexture = CreateTexture(cam);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> reflectCamera;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//设置反射相机的参数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">HoldCameraSettings</span>(<span class="params">Camera heplerCam</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        heplerCam.backgroundColor = Color.black;</span><br><span class="line">        heplerCam.clearFlags = _reflectSkybox ? CameraClearFlags.Skybox : CameraClearFlags.SolidColor;</span><br><span class="line">        heplerCam.renderingPath = RenderingPath.Forward;</span><br><span class="line">        heplerCam.cullingMask = _reflectionMask;</span><br><span class="line">        heplerCam.allowMSAA = <span class="literal">false</span>;</span><br><span class="line">        heplerCam.enabled = <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//创建RT </span></span><br><span class="line">    <span class="function">RenderTexture <span class="title">CreateTexture</span>(<span class="params">Camera sourceCam</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">int</span> width = Mathf.RoundToInt(Screen.width / _downsample);</span><br><span class="line">        <span class="built_in">int</span> height = Mathf.RoundToInt(Screen.height / _downsample);</span><br><span class="line">        RenderTextureFormat formatRT = sourceCam.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;</span><br><span class="line">        RenderTexture rt = <span class="keyword">new</span> RenderTexture(width, height, <span class="number">24</span>, formatRT);</span><br><span class="line">        rt.hideFlags = HideFlags.DontSave;</span><br><span class="line">        <span class="keyword">return</span> rt;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//内置回调函数，物体渲染之前会先调用该函数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnWillRenderObject</span>()</span> &#123;</span><br><span class="line">        Camera currentCam = Camera.current;</span><br><span class="line">        <span class="keyword">if</span> (currentCam == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> !UNITY_EDITOR</span></span><br><span class="line">        <span class="keyword">if</span> (!currentCam.gameObject.CompareTag(<span class="string">&quot;MainCamera&quot;</span>))</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (_insideRendering) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        _insideRendering = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (_reflectionCamera == <span class="literal">null</span>) &#123;</span><br><span class="line">            _reflectionCamera = CreateReflectionCamera(currentCam);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//渲染反射图</span></span><br><span class="line">        RenderReflection(currentCam, _reflectionCamera);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//是否对反射图进行模糊</span></span><br><span class="line">        <span class="keyword">if</span> (_reflectionCamera &amp;&amp; _sharedMaterial) &#123;</span><br><span class="line">            <span class="keyword">if</span> (_blurOn) &#123;</span><br><span class="line">                <span class="keyword">if</span> (_bluredReflectionTexture == <span class="literal">null</span>)</span><br><span class="line">                    _bluredReflectionTexture = CreateTexture(currentCam);</span><br><span class="line">                PostProcessTexture(currentCam, _reflectionCamera.targetTexture, _bluredReflectionTexture);</span><br><span class="line">                _sharedMaterial.SetTexture(_reflectionTex, _bluredReflectionTexture);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                _sharedMaterial.SetTexture(_reflectionTex, _reflectionCamera.targetTexture);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        _insideRendering = <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//调用反射相机，渲染反射图 </span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">RenderReflection</span>(<span class="params">Camera currentCam, Camera reflectCamera</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (reflectCamera == <span class="literal">null</span>) &#123;</span><br><span class="line">            Debug.LogError(<span class="string">&quot;反射Camera无效&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (_sharedMaterial &amp;&amp; !_sharedMaterial.HasProperty(_reflectionTex))</span><br><span class="line">        &#123;</span><br><span class="line">            Debug.LogError(<span class="string">&quot;Shader中缺少_ReflectionTex属性&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//保持反射相机的参数</span></span><br><span class="line">        HoldCameraSettings(reflectCamera);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (_reflectSkybox) &#123;</span><br><span class="line">            <span class="keyword">if</span> (currentCam.gameObject.GetComponent(<span class="keyword">typeof</span>(Skybox))) &#123;</span><br><span class="line">                Skybox sb = (Skybox)reflectCamera.gameObject.GetComponent(<span class="keyword">typeof</span>(Skybox));</span><br><span class="line">                <span class="keyword">if</span> (!sb) &#123;</span><br><span class="line">                    sb = (Skybox)reflectCamera.gameObject.AddComponent(<span class="keyword">typeof</span>(Skybox));</span><br><span class="line">                &#125;</span><br><span class="line">                sb.material = ((Skybox)currentCam.GetComponent(<span class="keyword">typeof</span>(Skybox))).material;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">bool</span> isInvertCulling = GL.invertCulling;</span><br><span class="line">        GL.invertCulling = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">        Transform reflectiveSurface = <span class="keyword">this</span>.transform; <span class="comment">//waterHeight;</span></span><br><span class="line"></span><br><span class="line">        Vector3 eulerA = currentCam.transform.eulerAngles;</span><br><span class="line"></span><br><span class="line">        reflectCamera.transform.eulerAngles = <span class="keyword">new</span> Vector3(-eulerA.x, eulerA.y, eulerA.z);</span><br><span class="line">        reflectCamera.transform.position = currentCam.transform.position;</span><br><span class="line"></span><br><span class="line">        Vector3 pos = reflectiveSurface.transform.position;</span><br><span class="line">        pos.y = reflectiveSurface.position.y;</span><br><span class="line">        Vector3 normal = reflectiveSurface.transform.up;</span><br><span class="line">        <span class="built_in">float</span> d = -Vector3.Dot(normal, pos) - _clipPlaneOffset;</span><br><span class="line">        Vector4 reflectionPlane = <span class="keyword">new</span> Vector4(normal.x, normal.y, normal.z, d);</span><br><span class="line"></span><br><span class="line">        Matrix4x4 reflection = Matrix4x4.zero;</span><br><span class="line">        reflection = CalculateReflectionMatrix(reflection, reflectionPlane);</span><br><span class="line">        _oldpos = currentCam.transform.position;</span><br><span class="line">        Vector3 newpos = reflection.MultiplyPoint(_oldpos);</span><br><span class="line"></span><br><span class="line">        reflectCamera.worldToCameraMatrix = currentCam.worldToCameraMatrix * reflection;</span><br><span class="line"></span><br><span class="line">        Vector4 clipPlane = CameraSpacePlane(reflectCamera, pos, normal, <span class="number">1.0f</span>);</span><br><span class="line"></span><br><span class="line">        Matrix4x4 projection = currentCam.projectionMatrix;</span><br><span class="line">        projection = CalculateObliqueMatrix(projection, clipPlane);</span><br><span class="line">        reflectCamera.projectionMatrix = projection;</span><br><span class="line"></span><br><span class="line">        reflectCamera.transform.position = newpos;</span><br><span class="line">        Vector3 euler = currentCam.transform.eulerAngles;</span><br><span class="line">        reflectCamera.transform.eulerAngles = <span class="keyword">new</span> Vector3(-euler.x, euler.y, euler.z);</span><br><span class="line"></span><br><span class="line">        reflectCamera.Render();</span><br><span class="line"></span><br><span class="line">        GL.invertCulling = isInvertCulling;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> Matrix4x4 <span class="title">CalculateObliqueMatrix</span>(<span class="params">Matrix4x4 projection, Vector4 clipPlane</span>)</span> &#123;</span><br><span class="line">        Vector4 q = projection.inverse * <span class="keyword">new</span> Vector4(</span><br><span class="line">            Mathf.Sign(clipPlane.x),</span><br><span class="line">            Mathf.Sign(clipPlane.y),</span><br><span class="line">            <span class="number">1.0F</span>,</span><br><span class="line">            <span class="number">1.0F</span></span><br><span class="line">            );</span><br><span class="line">        Vector4 c = clipPlane * (<span class="number">2.0F</span> / (Vector4.Dot(clipPlane, q)));</span><br><span class="line">        <span class="comment">// third row = clip plane - fourth row</span></span><br><span class="line">        projection[<span class="number">2</span>] = c.x - projection[<span class="number">3</span>];</span><br><span class="line">        projection[<span class="number">6</span>] = c.y - projection[<span class="number">7</span>];</span><br><span class="line">        projection[<span class="number">10</span>] = c.z - projection[<span class="number">11</span>];</span><br><span class="line">        projection[<span class="number">14</span>] = c.w - projection[<span class="number">15</span>];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> projection;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> Matrix4x4 <span class="title">CalculateReflectionMatrix</span>(<span class="params">Matrix4x4 reflectionMat, Vector4 plane</span>)</span> &#123;</span><br><span class="line">        reflectionMat.m00 = (<span class="number">1.0F</span> - <span class="number">2.0F</span> * plane[<span class="number">0</span>] * plane[<span class="number">0</span>]);</span><br><span class="line">        reflectionMat.m01 = (<span class="number">-2.0F</span> * plane[<span class="number">0</span>] * plane[<span class="number">1</span>]);</span><br><span class="line">        reflectionMat.m02 = (<span class="number">-2.0F</span> * plane[<span class="number">0</span>] * plane[<span class="number">2</span>]);</span><br><span class="line">        reflectionMat.m03 = (<span class="number">-2.0F</span> * plane[<span class="number">3</span>] * plane[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">        reflectionMat.m10 = (<span class="number">-2.0F</span> * plane[<span class="number">1</span>] * plane[<span class="number">0</span>]);</span><br><span class="line">        reflectionMat.m11 = (<span class="number">1.0F</span> - <span class="number">2.0F</span> * plane[<span class="number">1</span>] * plane[<span class="number">1</span>]);</span><br><span class="line">        reflectionMat.m12 = (<span class="number">-2.0F</span> * plane[<span class="number">1</span>] * plane[<span class="number">2</span>]);</span><br><span class="line">        reflectionMat.m13 = (<span class="number">-2.0F</span> * plane[<span class="number">3</span>] * plane[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">        reflectionMat.m20 = (<span class="number">-2.0F</span> * plane[<span class="number">2</span>] * plane[<span class="number">0</span>]);</span><br><span class="line">        reflectionMat.m21 = (<span class="number">-2.0F</span> * plane[<span class="number">2</span>] * plane[<span class="number">1</span>]);</span><br><span class="line">        reflectionMat.m22 = (<span class="number">1.0F</span> - <span class="number">2.0F</span> * plane[<span class="number">2</span>] * plane[<span class="number">2</span>]);</span><br><span class="line">        reflectionMat.m23 = (<span class="number">-2.0F</span> * plane[<span class="number">3</span>] * plane[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        reflectionMat.m30 = <span class="number">0.0F</span>;</span><br><span class="line">        reflectionMat.m31 = <span class="number">0.0F</span>;</span><br><span class="line">        reflectionMat.m32 = <span class="number">0.0F</span>;</span><br><span class="line">        reflectionMat.m33 = <span class="number">1.0F</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> reflectionMat;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">Vector4 <span class="title">CameraSpacePlane</span>(<span class="params">Camera cam, Vector3 pos, Vector3 normal, <span class="built_in">float</span> sideSign</span>)</span> &#123;</span><br><span class="line">        Vector3 offsetPos = pos + normal * _clipPlaneOffset;</span><br><span class="line">        Matrix4x4 m = cam.worldToCameraMatrix;</span><br><span class="line">        Vector3 cpos = m.MultiplyPoint(offsetPos);</span><br><span class="line">        Vector3 cnormal = m.MultiplyVector(normal).normalized * sideSign;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对反射图进行图像处理(利用command buffer实现)</span></span><br><span class="line">    <span class="keyword">private</span> Dictionary&lt;Camera, CommandBuffer&gt; _cameras = <span class="keyword">new</span> Dictionary&lt;Camera, CommandBuffer&gt;();</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">PostProcessTexture</span>(<span class="params">Camera cam, RenderTexture source, RenderTexture dest</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//参数有变化需要刷新commandbuffer</span></span><br><span class="line">        <span class="keyword">if</span> (_blurParamChanged)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (_cameras.ContainsKey(cam))</span><br><span class="line">                cam.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, _cameras[cam]);</span><br><span class="line">            _cameras.Remove(cam);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//已经设置了commandbuffer就不用再执行了</span></span><br><span class="line">        <span class="keyword">if</span> (_cameras.ContainsKey(cam))</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        CommandBuffer buf = <span class="keyword">new</span> CommandBuffer();</span><br><span class="line">        buf.name = <span class="string">&quot;Blur Reflection Texture&quot;</span>;</span><br><span class="line">        _cameras[cam] = buf;</span><br><span class="line">        <span class="built_in">float</span> width = source.width;</span><br><span class="line">        <span class="built_in">float</span> height = source.height;</span><br><span class="line">        <span class="built_in">int</span> rtW = Mathf.RoundToInt(width / _downsample);</span><br><span class="line">        <span class="built_in">int</span> rtH = Mathf.RoundToInt(height / _downsample);</span><br><span class="line"></span><br><span class="line">        <span class="built_in">int</span> blurredID = Shader.PropertyToID(<span class="string">&quot;_Temp1&quot;</span>);</span><br><span class="line">        <span class="built_in">int</span> blurredID2 = Shader.PropertyToID(<span class="string">&quot;_Temp2&quot;</span>);</span><br><span class="line">        buf.GetTemporaryRT(blurredID, rtW, rtH, <span class="number">0</span>, FilterMode.Bilinear, source.format);</span><br><span class="line">        buf.GetTemporaryRT(blurredID2, rtW, rtH, <span class="number">0</span>, FilterMode.Bilinear, source.format);</span><br><span class="line"></span><br><span class="line">        buf.Blit((Texture)source, blurredID);</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _blurIterations; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">float</span> iterationOffs = (i * <span class="number">1.0f</span>);</span><br><span class="line">            buf.SetGlobalFloat(<span class="string">&quot;_Offset&quot;</span>, iterationOffs / _downsample + _blurSize);</span><br><span class="line">            buf.Blit(blurredID, blurredID2, BlurMaterial, <span class="number">0</span>);</span><br><span class="line">            buf.Blit(blurredID2, blurredID, BlurMaterial, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        buf.Blit(blurredID, dest);</span><br><span class="line"></span><br><span class="line">        buf.ReleaseTemporaryRT(blurredID);</span><br><span class="line">        buf.ReleaseTemporaryRT(blurredID2);</span><br><span class="line"></span><br><span class="line">        cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, buf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="水面扰动"><a href="#水面扰动" class="headerlink" title="水面扰动"></a>水面扰动</h2><p><img src="https://img-blog.csdnimg.cn/f30a6465be3141829dec8b99a9876e59.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/8b82a9ba628346a6bfa38aeee17dd77f.png" alt="在这里插入图片描述"><br>下面的Vertex pos 是为了降低远处波动。<br>效果<br><img src="https://img-blog.csdnimg.cn/9b409dc253f14879a3071c337468789b.png" alt="在这里插入图片描述"></p>
<p>代码实现<br>粗糙用代码还原了一下</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line">Shader <span class="string">&quot;WaterCode&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">    Properties</span><br><span class="line">    &#123;</span><br><span class="line">        _ReflectionTex(<span class="string">&quot;ReflectionTex&quot;</span>, <span class="number">2</span>D) = <span class="string">&quot;white&quot;</span> &#123;&#125;</span><br><span class="line">    _WaterNormal(<span class="string">&quot;WaterNormal&quot;</span>, <span class="number">2</span>D) = <span class="string">&quot;white&quot;</span> &#123;&#125;</span><br><span class="line">    _WaterSpeed1(<span class="string">&quot;WaterSpeed1&quot;</span>, Float) = <span class="number">0.1</span></span><br><span class="line">    _NormalTilling(<span class="string">&quot;NormalTilling&quot;</span>, Float) = <span class="number">8</span></span><br><span class="line">    _WaterNoise(<span class="string">&quot;WaterNoise&quot;</span>, Float) = <span class="number">1</span></span><br><span class="line">    _Vector0(<span class="string">&quot;Vector 0&quot;</span>, Vector) = (<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    SubShader</span><br><span class="line">    &#123;</span><br><span class="line">        Tags &#123; <span class="string">&quot;RenderType&quot;</span>=<span class="string">&quot;Opaque&quot;</span> &#125;</span><br><span class="line">        LOD <span class="number">100</span></span><br><span class="line"></span><br><span class="line">        Pass</span><br><span class="line">        &#123;</span><br><span class="line">            CGPROGRAM</span><br><span class="line">            <span class="meta">#<span class="keyword">pragma</span> vertex vert</span></span><br><span class="line">            <span class="meta">#<span class="keyword">pragma</span> fragment frag</span></span><br><span class="line">            <span class="comment">// make fog work</span></span><br><span class="line">            <span class="meta">#<span class="keyword">pragma</span> multi_compile_fog</span></span><br><span class="line"></span><br><span class="line">            <span class="meta">#<span class="keyword">include</span> <span class="string">&quot;UnityCG.cginc&quot;</span></span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">struct</span> appdata</span><br><span class="line">            &#123;</span><br><span class="line">                float4 vertex : POSITION;</span><br><span class="line">                float2 uv : TEXCOORD0;</span><br><span class="line">                float4 tangent : TANGENT;</span><br><span class="line">        float3 normal : NORMAL;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            <span class="class"><span class="keyword">struct</span> <span class="title">v2f</span></span></span><br><span class="line"><span class="class">            &#123;</span></span><br><span class="line">                float2 texcoord0 : TEXCOORD0;</span><br><span class="line">                </span><br><span class="line">                float4 pos : SV_POSITION;</span><br><span class="line">                float4 screenPos:TEXCOORD1;</span><br><span class="line">                float3 worldPos:TEXCOORD2;</span><br><span class="line">                float3 worldTangent : TEXCOORD3;</span><br><span class="line">        float3 worldNormal : TEXCOORD4;</span><br><span class="line">        float3 worldBitangent : TEXCOORD5;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            sampler2D _ReflectionTex;</span><br><span class="line">            float4 _ReflectionTex_ST;</span><br><span class="line">            sampler2D _WaterNormal;</span><br><span class="line">            <span class="type">float</span> _NormalTilling;</span><br><span class="line">            <span class="type">float</span> _WaterSpeed1;</span><br><span class="line">            float4 _Vector0;</span><br><span class="line">            <span class="type">float</span> _WaterNoise;</span><br><span class="line"></span><br><span class="line">            v2f <span class="title function_">vert</span> <span class="params">(appdata v)</span></span><br><span class="line">            &#123;</span><br><span class="line">                v2f o;</span><br><span class="line">                o.pos = UnityObjectToClipPos(v.vertex);</span><br><span class="line">                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;</span><br><span class="line">                o.worldTangent    =  normalize( mul(unity_ObjectToWorld, v.tangent).xyz);</span><br><span class="line">                o.worldNormal     =  normalize( mul(unity_ObjectToWorld, float4(v.normal,<span class="number">0.0</span>)).xyz);</span><br><span class="line">                o.worldBitangent  = normalize(cross(o.worldNormal,o.worldTangent)*v.tangent.w);</span><br><span class="line">                o.screenPos = ComputeScreenPos(o.pos);</span><br><span class="line">                o.texcoord0 = v.uv;</span><br><span class="line">                <span class="keyword">return</span> o;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            fixed4 <span class="title function_">frag</span> <span class="params">(v2f i)</span> : SV_Target</span><br><span class="line">            &#123;</span><br><span class="line">                half4 screenPos = i.screenPos;</span><br><span class="line">                float3 worldTangent = normalize(i.worldTangent);</span><br><span class="line">        float3 worldNormal = normalize(i.worldNormal);</span><br><span class="line">        float3 worldBitangent = normalize(i.worldBitangent);</span><br><span class="line">                float3 tanToWorld0 = float3( worldTangent.x, worldBitangent.x, worldNormal.x );</span><br><span class="line">        float3 tanToWorld1 = float3( worldTangent.y, worldBitangent.y, worldNormal.y );</span><br><span class="line">        float3 tanToWorld2 = float3( worldTangent.z, worldBitangent.z, worldNormal.z );</span><br><span class="line">                half2 uv1_WaterNormal = i.worldPos.xz/_NormalTilling + _Time*_WaterSpeed1;</span><br><span class="line">                half2 uv2_WaterNormal = i.worldPos.xz/_NormalTilling - _Time*_WaterSpeed1;</span><br><span class="line">                half3 waterNormal = (tex2D(_WaterNormal,uv1_WaterNormal)+tex2D(_WaterNormal,uv2_WaterNormal)).xyz*<span class="number">0.5f</span>;</span><br><span class="line">                waterNormal.z =<span class="built_in">sqrt</span>( <span class="number">1.0f</span>- dot( waterNormal.xy,waterNormal.xy));</span><br><span class="line">                <span class="comment">//水面波动法线</span></span><br><span class="line">                waterNormal = float3(dot(tanToWorld0,waterNormal), dot(tanToWorld1,waterNormal), dot(tanToWorld2,waterNormal));</span><br><span class="line"></span><br><span class="line">                <span class="comment">//screenPos</span></span><br><span class="line">                screenPos = screenPos / screenPos.w;</span><br><span class="line">        screenPos.z = ( UNITY_NEAR_CLIP_VALUE &gt;= <span class="number">0</span> ) ? screenPos.z : screenPos.z * <span class="number">0.5</span> + <span class="number">0.5</span>;</span><br><span class="line"></span><br><span class="line">                half2 uv_result_reflection =  screenPos.xy+  (waterNormal.xy/ (i.pos.w+<span class="number">1</span>))*_WaterNoise;</span><br><span class="line"></span><br><span class="line">                <span class="comment">// sample the texture</span></span><br><span class="line">                fixed4 col = tex2D(_ReflectionTex,uv_result_reflection);</span><br><span class="line">                <span class="comment">// apply fog</span></span><br><span class="line">                <span class="keyword">return</span> col;</span><br><span class="line">            &#125;</span><br><span class="line">            ENDCG</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="折射效果"><a href="#折射效果" class="headerlink" title="折射效果"></a>折射效果</h2><p>用让水底贴图与水面反射贴图用菲尼尔因子做插值，当角度越垂直，水底越清晰。</p>
<h2 id="折射扭曲"><a href="#折射扭曲" class="headerlink" title="折射扭曲"></a>折射扭曲</h2><p>用WaterNoise做他的uv进行扰动，达到效果。</p>
<h2 id="水底高度"><a href="#水底高度" class="headerlink" title="水底高度"></a>水底高度</h2><p>给水底做视差贴图的效果，造成视差高度的效果。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="/music/jucilang.mp3">
            </audio>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='99f95a936c6fcdb1bd67'
        data-cs='c2d1bba3d6b7f9ade4f3de09a63925bd837c25ea'
        data-r='mrchenlearnspace.github.io'
        data-o='MrChenLearnSpace'
        data-a='MrChenLearnSpace'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%A9%E7%A9%BA%E7%90%83"><span class="toc-number">1.</span> <span class="toc-text">天空球</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B0%B4%E9%9D%A2"><span class="toc-number">2.</span> <span class="toc-text">水面</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B0%B4%E9%9D%A2%E5%8F%8D%E5%B0%84"><span class="toc-number">2.1.</span> <span class="toc-text">水面反射</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B0%B4%E9%9D%A2%E6%89%B0%E5%8A%A8"><span class="toc-number">2.2.</span> <span class="toc-text">水面扰动</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8A%98%E5%B0%84%E6%95%88%E6%9E%9C"><span class="toc-number">2.3.</span> <span class="toc-text">折射效果</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8A%98%E5%B0%84%E6%89%AD%E6%9B%B2"><span class="toc-number">2.4.</span> <span class="toc-text">折射扭曲</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B0%B4%E5%BA%95%E9%AB%98%E5%BA%A6"><span class="toc-number">2.5.</span> <span class="toc-text">水底高度</span></a></li></ol></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/blog/js/plugin.js"></script>
<script src="/blog/js/typed.js"></script>
<script src="/blog/js/diaspora.js"></script>


<link rel="stylesheet" href="/blog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/blog/photoswipe/default-skin/default-skin.css">


<script src="/blog/photoswipe/photoswipe.min.js"></script>
<script src="/blog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YMJ8CBH8F7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-YMJ8CBH8F7');
</script>
<!-- End Google Analytics -->


</html>
